#include "nn_common.hpp"

#include "cv_common.hpp"

namespace TRT {

void TRTLogger::log(Severity severity, const char *msg) noexcept {
  if (severity == Severity::kINTERNAL_ERROR) {
    spdlog::error(msg);
  } else if (severity == Severity::kERROR) {
    spdlog::error(msg);
  } else if (severity == Severity::kWARNING) {
    spdlog::warn(msg);
  } else if (severity == Severity::kINFO) {
    spdlog::info(msg);
  } else if (severity == Severity::kVERBOSE) {
    spdlog::debug(msg);
  }
}

int TRTLogger::GetVerbosity() { return (int)Severity::kVERBOSE; }

}  // namespace TRT

namespace component {

Model FloatToModel(float class_id) {
  if (class_id == 0)
    return Model::NoMask;
  else if (class_id == 1)
    return Model::WithMask;
  else
    return Model::WithMask;
}

std::string ModelToString(Model model) {
  if (model == Model::WithMask)
    return std::string("WithMask");
  else if (model == Model::NoMask)
    return std::string("NoMask");
  else
    return std::string("WithMask");
}

void Draw(const cv::Mat &output, TRT::Detection detection, int id) {
  auto l = detection.x_ctr - detection.w / 2.f;
  auto r = detection.x_ctr + detection.w / 2.f;
  auto t = detection.y_ctr - detection.h / 2.f;
  auto b = detection.y_ctr + detection.h / 2.f;
  cv::Rect rect(l, t, r - l, b - t);
  auto color = detection.class_id == 0 ? painting::kRED : painting::kGREEN;
  cv::rectangle(output, rect, color, 2);
  cv::putText(output,
              ModelToString(FloatToModel(detection.class_id)) + " " +
                  std::to_string(id),
              cv::Point2i(l, t), painting::kCV_FONT, 0.9, color, 2);
}

void PrintInfo(TRT::Detection detection) {
  // SPDLOG_INFO("[x_ctr] : {}", detection.x_ctr);
  // SPDLOG_INFO("[y_ctr] : {}", detection.y_ctr);
  // SPDLOG_INFO("[w] : {}", detection.w);
  // SPDLOG_INFO("[h] : {}", detection.h);
  // SPDLOG_INFO("[conf] : {}", detection.conf);
  // SPDLOG_INFO("[class_id] : {}", detection.class_id);
  SPDLOG_INFO("{}, {}, {}, {}, {}, {}", detection.x_ctr, detection.y_ctr,
              detection.h, detection.w, detection.conf, detection.class_id);
}

std::string Replace(std::string orin, const std::string &add) {
  int npos = orin.find("test");
  std::string file_name = orin.replace(npos, npos + 4, add) +
                          orin.substr(npos + 4, orin.size() - npos);

  SPDLOG_WARN("file_name : {}", file_name);
  return file_name;
}

}  // namespace component
